package com.base.myproject.client.busi.ccode;

import java.util.ArrayList;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.user.client.ui.Label;

public class Test extends BaseJDBCPanel {

	BaseStore<BusiObject> store = null;

	public Test() {
		super();
		init();
	}

	public void init() {
		this.add(initWeiget());
	}

	public ContentPanel initWeiget() {
		ContentPanel panel = new ContentPanel();
		panel.add(createGrid());
		panel.add(createForm());
		return panel;
	}

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("b2cgcode");
		type.getField("b2cgcode").setType(String.class);
		type.addField("b2cgname");
		type.getField("b2cgname").setType(String.class);
		type.addField("gcode");
		type.getField("gcode").setType(String.class);
		type.addField("gname");
		type.getField("gname").setType(String.class);
		type.addField("mainimagepath");
		type.getField("mainimagepath").setType(String.class);
		type.addField("detailimageidx");
		type.getField("detailimageidx").setType(String.class);
		type.addField("treecode");
		type.getField("treecode").setType(String.class);
		type.addField("recommend");
		type.getField("recommend").setType(String.class);
		type.addField("adddate");
		type.getField("adddate").setType(String.class);
		type.addField("price");
		type.getField("price").setType(String.class);
		type.addField("discountprice");
		type.getField("discountprice").setType(String.class);
		type.addField("suit");
		type.getField("suit").setType(Boolean.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("null");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select * from b2cgcode");
		store = new BaseStore<BusiObject>(type, info);
		return store;
	}

	BaseGrid grid = null;

	BaseGrid createGrid() {
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

		ColumnConfig C1 = new ColumnConfig("icode", "icode", 80);
		columns.add(C1);
		ColumnConfig C2 = new ColumnConfig("b2cgcode", "b2cgcode", 80);
		columns.add(C2);
		ColumnConfig C3 = new ColumnConfig("b2cgname", "b2cgname", 80);
		columns.add(C3);
		ColumnConfig C4 = new ColumnConfig("gcode", "gcode", 80);
		columns.add(C4);
		ColumnConfig C5 = new ColumnConfig("gname", "gname", 80);
		columns.add(C5);
		ColumnConfig C6 = new ColumnConfig("mainimagepath", "mainimagepath", 80);
		columns.add(C6);
		ColumnConfig C7 = new ColumnConfig("detailimageidx", "detailimageidx",
				80);
		columns.add(C7);
		ColumnConfig C8 = new ColumnConfig("treecode", "treecode", 80);
		columns.add(C8);
		ColumnConfig C9 = new ColumnConfig("recommend", "recommend", 80);
		columns.add(C9);
		ColumnConfig C10 = new ColumnConfig("adddate", "adddate", 80);
		columns.add(C10);
		ColumnConfig C11 = new ColumnConfig("price", "price", 80);
		columns.add(C11);
		ColumnConfig C12 = new ColumnConfig("discountprice", "discountprice",
				80);
		columns.add(C12);
		ColumnConfig C13 = new ColumnConfig("suit", "suit", 80);
		columns.add(C13);

		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid(getStore(), cm);
		return grid;
	}

	BaseFormPanel createForm() {
		BaseFormPanel form = new BaseFormPanel(getStore());
		TableLayout tl = new TableLayout(6);

		TableData td = new TableData();
		td.setRowspan(1);
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.TOP);
		td.setPadding(2);
		form.setLayout(tl);
		form.setHeaderVisible(false);

		form.add(new Label("icode:"), td);
		NumberField C_num1 = new NumberField();
		C_num1.setPropertyEditorType(Long.class);
		C_num1.setName("icode");
		form.add(C_num1, td);
		form.add(new Label("b2cgcode:"), td);
		TextField<String> C_num2 = new TextField<String>();
		C_num2.setName("b2cgcode");
		form.add(C_num2, td);
		form.add(new Label("b2cgname:"), td);
		TextField<String> C_num3 = new TextField<String>();
		C_num3.setName("b2cgname");
		form.add(C_num3, td);
		form.add(new Label("gcode:"), td);
		TextField<String> C_num4 = new TextField<String>();
		C_num4.setName("gcode");
		form.add(C_num4, td);
		form.add(new Label("gname:"), td);
		TextField<String> C_num5 = new TextField<String>();
		C_num5.setName("gname");
		form.add(C_num5, td);
		form.add(new Label("mainimagepath:"), td);
		TextField<String> C_num6 = new TextField<String>();
		C_num6.setName("mainimagepath");
		form.add(C_num6, td);
		form.add(new Label("detailimageidx:"), td);
		TextField<String> C_num7 = new TextField<String>();
		C_num7.setName("detailimageidx");
		form.add(C_num7, td);
		form.add(new Label("treecode:"), td);
		TextField<String> C_num8 = new TextField<String>();
		C_num8.setName("treecode");
		form.add(C_num8, td);
		form.add(new Label("recommend:"), td);
		TextField<String> C_num9 = new TextField<String>();
		C_num9.setName("recommend");
		form.add(C_num9, td);
		form.add(new Label("adddate:"), td);
		TextField<String> C_num10 = new TextField<String>();
		C_num10.setName("adddate");
		form.add(C_num10, td);
		form.add(new Label("price:"), td);
		TextField<String> C_num11 = new TextField<String>();
		C_num11.setName("price");
		form.add(C_num11, td);
		form.add(new Label("discountprice:"), td);
		TextField<String> C_num12 = new TextField<String>();
		C_num12.setName("discountprice");
		form.add(C_num12, td);
		form.add(new Label("suit:"), td);
		CheckBox C_num13 = new CheckBox();
		C_num13.setName("suit");
		form.add(C_num13, td);
		form.inited();
		return form;
	};

	int sync = 0;                                                      
	Listener<BaseEvent> defaultlistener;                               
                                                                     
	Listener<BaseEvent> getListener() {                                
		if(defaultlistener!=null)                                        
		{                                                                
			return defaultlistener;                                        
		}                                                                
                                                                     
		defaultlistener = new Listener<BaseEvent>() {                    
			public void handleEvent(BaseEvent be) {                        
				sync++;                                                      
				try {                                                        
					if (sync == 1) {                                           
						if (be.getType() == Events.Select) {                     
							if (be.getSource().equals( "")) {                            
                                                                     
							}                                                      
						} else if (be.getType() == Events.Change) {              
							if (be.getSource().equals("")) {         
                                                                     
							}                                                      
						} else if (be.getType() == Events.KeyUp) {               
							if (be instanceof ComponentEvent) {                    
								if (((ComponentEvent) be).getKeyCode() == 13) // 回车
								{                                                    
								}                                                    
							}                                                      
						}                                                        
					}                                                          
				} finally {                                                  
					sync--;                                                    
				}                                                            
			}                                                              
                                                                     
		};                                                               
		return defaultlistener;                                          
	}                                                                  

}
